﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Warehouse.BusinessLogic;
using Warehouse.UI.WinForm.Properties;

namespace Warehouse.UI.WinForm.Forms
{
    public partial class frmLogin : Form
    {
        private void _initCreate()
        {
            this.Text = Resources.WindowText_Login;
            ltbUserCode.LabelText = "用户编号";
            ltbUserPass.LabelText = "密码";
        }

        public frmLogin()
        {
            InitializeComponent();
            _initCreate();
        }
        public frmLogin(string usercode, string passwd)
            : this()
        {
            ltbUserCode.TextBoxText = usercode;
            ltbUserPass.TextBoxText = passwd;
        }

        private bool _login(string usercode, string passwd)
        {
            var bl = new UserBL();
            return bl.Login(usercode, passwd);
        }

        private bool _checkValid()
        {
            ltbUserCode.TextBoxText = ltbUserCode.TextBoxText.Trim();
            if (ltbUserCode.TextBoxText == "")
            {
                ltbUserCode.Focus();
                return false;
            }

            ltbUserPass.TextBoxText = ltbUserPass.TextBoxText.Trim();
            if (ltbUserPass.TextBoxText == "")
            {
                ltbUserPass.Focus();
                return false;
            }

            return true;
        }

        private void pnlOkCancel1_btnOkClick(object sender, EventArgs e)
        {
            if (_checkValid())
            {

                if (_login(ltbUserCode.TextBoxText, ltbUserPass.TextBoxText) == true)
                {
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    //Utility.ShowError("用户名或密码不正确", this);
                    ltbUserCode.Focus();
                }
            }
        }

        private void pnlOkCancel1_btnCancelClick(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void frmLogin_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == Convert.ToChar(13))
            {
                e.Handled = true;
                SendKeys.Send("{TAB}");

                if (ltbUserPass.ContainsFocus && _checkValid())
                    pnlOkCancel1_btnOkClick(sender, e);
            }
        }
    }
}
